var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.get('/', function(req, res, next) {
    let equipmentid = _.trim(String(req.query.equipmentid));
    let state = _.trim(String(req.query.state));

    let data = {
        isSuccess: false,
        data: null,
        msg: '资产状态数据获取失败！'
    };
    let whereClause = "equipmentid='" + equipmentid + "'";
    let tableName = ''
    switch(state) {
        case '1':
            tableName = 'equipmentinfo';
            break;
        case '2':
            tableName = 'event_record';
            whereClause = whereClause + " and isfinished=0";
            break;
        case '3':
            tableName = 'equipment_maintenance';
            whereClause = whereClause + " and isfinished=0";
            break;
    }
    let sql = 'select * from ' + tableName + ' where ' + whereClause;
    console.log( "获取维修信息：" + sql);
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            if(rows.length == 1 ) {
                data.data = rows[0];
                data.isSuccess = true;
                data.msg = "资产状态数据获取成功！";
                console.log(data.data)
                return res.json(data);
            } else if(rows.length < 0){
                data.msg = "资产状态数据获取失败！"
                return res.json(data);
            } else {
                data.msg = "资产状态数据出现错误，请联系管理员！"
                return res.json(data);
            }
        }
    })
});

module.exports = router;